<?php
include($_SERVER["DOCUMENT_ROOT"] . "/bob.php");
include("fonctions_forum.php");
$mysqli = new_mysqli($host, $user, $password, $database); //connection à la DB

if (isset($_GET['id'])){
    $idfrum = get_num($_GET['id']);
    $fruum = get_line_in_db($mysqli, "forums", "forum_id", $idfrum);
    $forum_name=$fruum['forum_name'];



    if ($fruum == NULL) {
        header("Location: /");
        exit();
    }
    if ($fruum['auth_view'] == '2' && @$_SESSION['grad'] != "admin") { //Si c'est un forum privé on vérifie les autorisations. TODO : ceci est un doublon avec la vérif dans la fonction display_forum_ind()
        $result = mysqli_query_with_error($mysqli, "SELECT forum_id FROM forum_private WHERE forum_id ='$idfrum' AND user_id='" . get_num($_SESSION['id']) . "' LIMIT 1");
        if ($result->num_rows == 0) {
            header("Location: /");
            exit();
        }
    }
    $requete="";
    $texte_requete_vide="";
} else {
    $idfrum=-1;
    if(isset($_GET['filtre'])){
        switch($_GET['filtre']){
            case 'suivis':
                $forum_name="Mes sujets suivis";
                $requete="SELECT * FROM forum_topics as ft LEFT JOIN forum_topics_suivis as fts ON ft.topic_id=fts.topic_id WHERE fts.user_id=".$_SESSION['id']." ORDER BY topic_type DESC, topic_time DESC";
                $texte_requete_vide="<p>Vous ne suivez aucun sujet.</p>";
                break;
            case 'mes_sujets':
                $forum_name="Mes sujets";
                $requete="SELECT * FROM forum_topics WHERE topic_poster=".$_SESSION['id']." ORDER BY topic_type DESC, topic_time DESC";
                $texte_requete_vide="<p>Vous n'avez pas encore créer de sujet.</p>";
                break;
            case 'sans_reponse':
                $forum_name="Sujets sans réponse";
                $requete="SELECT ft.* FROM forum_topics as ft LEFT JOIN forum_posts as fp ON  ft.topic_id=fp.topic_id GROUP BY ft.topic_id HAVING COUNT(DISTINCT fp.post_id)<2 ORDER BY topic_type DESC, topic_time DESC";
                $texte_requete_vide="<p>Il n'y a aucun sujet sans réponse pour le moment.</p>";
                break;
            case 'non_lus':
                $forum_name="Sujets non lus";
                $cond="topic_id='0' ";
                if(isset($_SESSION['new_topics'])) {
                    foreach ($_SESSION['new_topics'] as $topic_id => $forum_id){                        
                        $cond=$cond."OR topic_id='".substr($topic_id, 6)."' "; //Les clefs sont de la forme "topic_45", 45 étant l'id du topic.
                    }
                }

                $requete= "SELECT * FROM forum_topics WHERE $cond ORDER BY topic_time DESC";
                //$requete="SELECT ft.* FROM forum_topics as ft LEFT JOIN forum_posts as fp ON  ft.topic_id=fp.topic_id GROUP BY ft.topic_id HAVING COUNT(DISTINCT fp.post_id)<2 ORDER BY topic_type DESC, topic_time DESC";
                $texte_requete_vide="<p>Il n'y a aucun sujet non lu.</p>";
                break;
            default:
                break;
        }
    }
}

entete($mysqli, $forum_name);

//$result = mysqli_query_with_error($mysqli, "SELECT * FROM forum_topics WHERE forum_id='$idfrum' ORDER BY topic_type DESC, topic_time DESC");
//$data_pages = pagination(30, $result->num_rows, "forum.php?id=$idfrum");
//if ($data_pages == NULL) {
//    header("Location: /");
//    exit();
//}
//display_forums($mysqli, $idfrum, true); //sous-forums

    if (isset($_GET['page'])){
        $page=$_GET['page'];
    }else{
        $page=1;
    }
    display_forum_ind($mysqli, $idfrum,$page,$requete,$texte_requete_vide);

//echo 'salut';
$nb_current = 0;

?>

                <div id="legende" style="float: none" class="ombre">
                    <h4 class="ombre">Légende des icônes</h4>
                    <p>
                        <span class="nonlu">contient des messages non lus</span><br/>
                        <span class="lu">ne contient pas de messages non lus</span><br/>
                        <span class="verrouille">discussion verouillée</span>
                    </p>
                </div>
            </div> <!-- fin div contenu-->
        </div> <!-- fin div corps-->




<?php
show_footer();
?>
</div></body></html>
